//
//  ViewController.m
//  10804省会和市和区
//
//  Created by student on 16/2/28.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic,strong)NSMutableArray *Cities;
@property(nonatomic,strong)NSMutableArray *city;
@property(nonatomic,strong)NSMutableArray *Province;
@property(nonatomic,strong)NSMutableArray *Area;
@property(nonatomic,strong)NSDictionary *dict1;
@property(nonatomic,strong)NSDictionary *dict2;
@property(nonatomic,strong)NSDictionary *dict3;
@property(nonatomic,strong)NSDictionary *dict4;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.Province=[NSMutableArray array];
    self.Cities=[NSMutableArray array];
    self.city=[NSMutableArray array];
    self.Area=[NSMutableArray array];
   
    NSString *path=[[NSBundle mainBundle]pathForResource:@"area2" ofType:@"plist"];
    self.dict1=[NSDictionary dictionaryWithContentsOfFile:path];
    for (int i =0; i<self.dict1.allKeys.count; i++) {
        NSString *str=[NSString stringWithFormat:@"%i",i];
        self.dict2 = self.dict1[str];
        [self.Province addObject:[self.dict2 allKeys]];
        
    }
    
    
    self.dict2=self.dict1[@"0"];
    self.dict3=self.dict2[[self.dict2 allKeys]];
    for (int i=0; self.dict3.allKeys.count;i++) {
        self.dict4=self.dict3[[NSString stringWithFormat:@"%i",i]];
        [self.Cities addObject:[self.dict4 allKeys]];
    }
    
    
    
    self.dict2=self.dict1[@"0"];
    self.dict3=self.dict2[[self.dict2 allKeys]];
        self.dict4=self.dict3[@"0"];
        [self.Cities addObject:[self.dict4 allKeys]];
        for (int i=0; i<self.dict4.allKeys.count; i++) {
            self.city=self.dict4[[NSString stringWithFormat:@"%i",i]];
            [self.Area addObject:self.city];
        }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0) {
        return self.Province.count;
    }
    else if(component==1){
        return self.Cities.count;
    }
    else{
        return self.Area.count;
    }
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component==0) {
        return self.Province[row];
    }
    else if (component==1){
        return self.Cities[row];
    }
    else{
        return self.Area[row];
    }
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    [self.Province removeAllObjects];
    [self.Cities removeAllObjects];
    [self.city removeAllObjects];
    [self.Area removeAllObjects];
    if (component==0) {
        self.dict2=self.dict1[[NSString stringWithFormat:@"%li",(long)row]];
        self.dict3=self.dict2[[self.dict2 allKeys]];
        for (int i=0; self.dict3.allKeys.count;i++) {
            self.dict4=self.dict3[[NSString stringWithFormat:@"%i",i]];
            [self.Cities addObject:[self.dict4 allKeys]];
        }
    }
    else if (component==1) {
        for (int i=0; i<self.dict4.allKeys.count; i++) {
            self.city=self.dict4[[NSString stringWithFormat:@"%li",(long)row]];
            [self.Area addObject:self.city];
        }
    }
}
@end
